﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataAccess.Interface.Dao;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using System.Data;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class ContainerDaoImpl : BaseGenericDaoImpl
    {
        public bool DeleteObject(Object entity)
        {
            /*SpecEntity item = (SpecEntity)entity;
            string deleteSql = "delete from DUO_SPEC_ENTITY where CONTAINERID = '" + item.Id + "'";
            return  this.dbConn.Delete(deleteSql);
             * */
            return false;
        }

        public bool SaveOrUpdateObject(Object entity)
        {
            DeleteObject(entity);

            StringBuilder sb = new StringBuilder();
            SpecEntity item = (SpecEntity)entity;            
            sb.Clear();
            sb.Append(" insert into");
            sb.Append(" DUO_SPEC_ENTITY (ENTITYID, SPECID, SITECODE, ENTITYSTATE, FREEZETHRAWTIMIES, STOREDDATE)");
            sb.Append(" values(");
            sb.Append(" '" + item.Id + "',");
            sb.Append(" '" + item.SpecId + "',");
            sb.Append(" '" + item.SiteCode + "',");
            sb.Append(" " + item.EntityState.ToString() + ",");
            sb.Append(" " + item.FreezeThrawTimes.ToString() + ",");
            sb.Append("to_date ( '" + item.StoredDate.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' )");
            sb.Append(")");
            bool isSaved = this.dbConn.Insert(sb.ToString());
            return isSaved;
        }
        /**
  * 
  * <p>功能描述：保存容器数组</p>
  * <p>修改人:</p>
  * <p>修改时间:</p>
  * <p>修改描述:</p>
  * @param id
  * @return
  */


        public bool SaveContainers(ObservableCollection<ContainerEntity> containerList)
        {
            bool success = true;
            ContainerEntity ctnIndex;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < containerList.Count; i++)
            {
                ctnIndex = containerList[i];
                bool isSaved = SaveOrUpdateContainer(ctnIndex);
                success = success & isSaved;
            }
            return success;
        }

        public bool SaveOrUpdateContainer(ContainerEntity ctn)
        {
           // DeleteContainerById(ctn.Id);
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" insert into");
            sb.Append(" DUO_CONTAINER (CONTAINERID, CONTAINERMODELID, PARENTID, SITECODE, REMAININGCAPACITY, WHOLECAPACITY, DESCRIPTION, ROOTID, MODELTYPE)");
            sb.Append(" values(");
            sb.Append(" '" + ctn.Id + "',");
            sb.Append(" '" + ctn.ContainerModelId + "',");
            sb.Append(" '" + ctn.ParentId + "',");
            sb.Append(" '" + ctn.SiteCode + "',");
          //  sb.Append(" '" + ctn.ModelType + "',");
            sb.Append(" '" + ctn.RemainingCapacity.ToString() + "',");
            sb.Append(" '" + ctn.WholeCapacity.ToString() + "',");
            sb.Append(" '" + ctn.Description + "',");
            sb.Append(" '" + ctn.RootId + "',");
            sb.Append(" '" + ctn.ModelType + "'");
            sb.Append(")");
            return this.dbConn.Insert(sb.ToString());
        }

        public bool DeleteContainerById(string id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" delete ");
            sb.Append(" from DUO_CONTAINER");
            sb.Append(" where ROOTID = '" + id + "'");
            sb.Append(" or CONTAINERID = '" + id + "'");

            bool success = this.dbConn.Delete(sb.ToString());
            return success;
        }
        /**
        * 
        * <p>功能描述：获取所有容器定义</p>
        * <p>修改人:</p>
        * <p>修改时间:</p>
        * <p>修改描述:</p>
        * @param id
        * @return
        */
        public ObservableCollection<ContainerEntity> GetAllContainers()
        {
            ObservableCollection<ContainerEntity> specEntityList = new ObservableCollection<ContainerEntity>();
            ContainerEntity entity;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_CONTAINER");
            sb.Append(" where ROOTID = 'NULL'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    entity = new ContainerEntity();
                   // ctnItem.State = DataModel.RecordState.Normal;
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        entity.setValue(clmName, dt.Rows[j][clmName]);
                    }

                    specEntityList.Add(entity);
                }
                return specEntityList;
            }
            return null;
        }

        public ContainerEntity GetContainerEntity(string sitecode, string rootid)
        {
            ContainerEntity entity;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_CONTAINER");
            sb.Append(" where ROOTID = '" + rootid + "'");
            sb.Append(" and SITECODE = '" + sitecode + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    entity = new ContainerEntity();
                    // ctnItem.State = DataModel.RecordState.Normal;
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        entity.setValue(clmName, dt.Rows[j][clmName]);
                    }

                    return entity;
                }
            }
            return null;
        }

        /**
        * 
        * <p>功能描述：根据ID获取容器定义</p>
        * <p>修改人:</p>
        * <p>修改时间:</p>
        * <p>修改描述:</p>
        * @param id
        * @return
        */
        public List<SpecEntity> loadContainerById(string ctnId)
        {
            throw new NotImplementedException();
        }

        /**
        * 
        * <p>功能描述：根据名称获取容器定义</p>
        * <p>修改人:</p>
        * <p>修改时间:</p>
        * <p>修改描述:</p>
        * @param id
        * @return
        */
        public List<SpecEntity> loadContainerByName(string ctnName)
        {
            throw new NotImplementedException();
        }

        public bool DeleteSpecEntityById(string id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" delete ");
            sb.Append(" from DUO_SPEC_ENTITY");
            sb.Append(" where ENTITYID = '" + id + "'");

           bool success = this.dbConn.Delete(sb.ToString());
           return success;
        }


        public ContainerModelEntity GetContainerModel(string sitecode)
        {
            ContainerModelEntity cm = new ContainerModelEntity();
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select t2.NAME, t2.STYLEFILE, t2.CREATEDATE");
            sb.Append(" from DUO_SPEC_ENTITY t1, DUO_CONTAINER_MODEL t2");
            sb.Append(" where t1.MODELTYPE = t2.NAME");
            sb.Append(" and t1.SITECODE = '" + sitecode + "'");
            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    // ctnItem.State = DataModel.RecordState.Normal;
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        cm.setValue(clmName, dt.Rows[j][clmName]);
                    }
                }
                return cm;
            }
            return null;
        }

        public ContainerEntity GetContainerById(string id)
        {
            ContainerEntity entity = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_CONTAINER");
            sb.Append(" where CONTAINERID ='" + id + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    entity = new ContainerEntity();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        entity.setValue(clmName, dt.Rows[j][clmName]);
                    }

                }
                return entity;
            }
            else
                return null;
        }

        public ContainerEntity GetContainerBySitecode(string sitecode)
        {
            ContainerEntity entity = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_CONTAINER");
            sb.Append(" where SITECODE ='" + sitecode + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    entity = new ContainerEntity();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        entity.setValue(clmName, dt.Rows[j][clmName]);
                    }

                }
                return entity;
            }
            else
                return null;
        }

        public void UpdateCellState(string sitecode, double remainingCapacity)
        {
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" update DUO_CONTAINER");
            sb.Append(" set REMAININGCAPACITY = " + remainingCapacity + "");
            sb.Append(" where SITECODE = '" + sitecode + "'");
            this.dbConn.Update(sb.ToString());
        }

        public void UpdateContainerState(string sitecode, bool isAdded = true)
        {
            //int remainingCapacity = 0;
            UpdateCellState(sitecode, isAdded ? 0 : 1);

            ContainerEntity ce = GetContainerBySitecode(sitecode);

            while (ce != null && ce.ParentId != null)
            {
                ContainerEntity tmpCe = GetContainerById(ce.ParentId);
                UpdateCellState(tmpCe.SiteCode, isAdded ? tmpCe.RemainingCapacity - 1: tmpCe.RemainingCapacity + 1);
                ce = tmpCe;
            }
        }
        public List<ContainerEntity> GetChildContainersBySitecode(string sitecode)
        {
            List<ContainerEntity> list = new List<ContainerEntity>();
            ContainerEntity entity = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_CONTAINER");
            sb.Append(" where parentid in (");
            sb.Append(" select containerid from DUO_CONTAINER ");
            sb.Append(" where sitecode = '" + sitecode+ "'");
            sb.Append(" )");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    entity = new ContainerEntity();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        entity.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    list.Add(entity);
                }              
            }
            else
                return null;
            return list;
        }
        public List<ContainerEntity> GetChildContainers(string id)
        {
            List<ContainerEntity> list = new List<ContainerEntity>();
            ContainerEntity entity = null;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_CONTAINER");
            sb.Append(" where PARENTID ='" + id + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    entity = new ContainerEntity();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        entity.setValue(clmName, dt.Rows[j][clmName]);
                    }

                }
                list.Add(entity);
            }
            else
                return null;
            return list;
        }
    }
}
